<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\LiveMeetingRoom;
use App\Models\AdminUsers;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use App\Admin\Renderable\LiveMeetingUser;
use Illuminate\Support\Facades\Auth;

class LiveMeetingRoomController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new LiveMeetingRoom(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('room_name');
//            $grid->column('list_img')->image('',60,60);
            $grid->column('live_admin_id','主播人')->display(function($live_admin_id){
                $adminUser = AdminUsers::find($live_admin_id);
                return $adminUser ? ($adminUser -> name?: $adminUser -> username)  : '';
            });
            $grid->column('room_no');
            $grid->column('online_user_num')->modal(function($modal){
                $modal ->title('房间号：'.$this ->room_no);

                return liveMeetingUser::make(['title' => $this ->title]);
            });

            $grid->column('status') -> display(function($status){
                return $status == 1 ? '直播中' : '未直播';
            });

            $grid->column('操作')->display(function () {

                $editPage = admin_base_path('live_meeting_in_room?live_meeting_id='.$this -> id);
                $html = '';
                $html .= "<a class='btn-sm btn-primary' href='live_meeting_room/{$this->id}/edit' style='margin-right: 10px;'>
                    编辑
                         </a>";
                $html .= "
                         <a class='btn-sm btn-primary' href='{$editPage}' style='margin-right: 10px;' target='_blank'>
                            进入房间
                         </a>
                       ";
                return $html;

            })->width('200px');


            $grid->disableDeleteButton();
            $grid->disableViewButton();
            $grid->disableActions();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new LiveMeetingRoom(), function (Show $show) {
            $show->field('id');
            $show->field('room_name');
            $show->field('intro');
            $show->field('live_admin_id');
            $show->field('list_img')->image('',80,80);
            $show->field('room_secret');
            $show->field('room_no');

            $show->field('time_long');
            $show->field('room_num_limit');
            $show->field('online_user_num');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new LiveMeetingRoom(), function (Form $form) {

            $form->display('id');
            $form->text('room_name');

            if($form -> isCreating()){
                $form->hidden('room_user_no')->value('xtong_'.rand_str(6));
            }


//            $form->text('intro');
//            $form->image('list_img') ->move('live_meeting_room') -> autoUpload();


            if($form -> isCreating()){
                $admin_id = Auth::guard('admin')->user() -> id;
                $random = rand_str(6);
                $form->text('room_no')->value( \App\Models\LiveMeetingRoom::uniqudRoomNo() );
                $form->text('room_secret')->rules('required|alpha_num')->maxLength(6)->minLength(6)->help('请输入6位字母、数字的组合')->value($random);
                $form ->hidden('windows_publish_stream_id');
                $form ->hidden('audio_publish_stream_id');
                $form ->hidden('live_admin_id') ->value($admin_id);
            }else{
                $form->text('room_no') ->disable();
                $form->text('room_secret')->rules('required|alpha_num')->maxLength(6)->minLength(6)->help('请输入6位字母、数字的组合');

            }


            $form->number('room_num_limit')->attribute('min',1)->attribute('max',20) ->help('人数限制在20人');

            $form->radio('status')->options([ 1 => '开启', 0 => '关闭']) ->default(0);
//            $form ->hidden('status') ->value(0);
            $form->display('created_at');
            $form->display('updated_at');

            $form ->disableDeleteButton();
            $form ->disableViewButton();

            $form ->submitted(function($form){

                if($form->isCreating()){
                    $form ->windows_publish_stream_id = 'video_'.rand_str(6);
                    $form ->audio_publish_stream_id = 'audio_'.rand_str(4);
                }

            });
        });
    }
}
